package com.ruoyi.app.business.project;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.app.config.util.UserUtil;
import com.ruoyi.app.domain.project.ProjectSimple;
import com.ruoyi.app.domain.project.favoirite.FavouriteSave;
import com.ruoyi.common.core.enums.YesNoEnum;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.service.project.entity.LaborFavouriteEntity;
import com.ruoyi.service.project.query.FavouriteBaseQuery;
import com.ruoyi.service.project.service.LaborFavouriteService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * <p>
 * 作用：收藏项目管理
 * </p>
 *
 * @author Zane
 * @since 2025-10-10 2:19
 */
@Slf4j
@Component
public class FavouriteBusiness {

  @Resource
  private LaborFavouriteService laborFavouriteService;

  @Lazy
  @Resource
  private ProjectBusiness projectBusiness;

  @Resource
  private TransactionTemplate transactionTemplate;

  public CommonPageInfo<ProjectSimple> favouriteList(FavouriteBaseQuery query) {
    final Long userId = UserUtil.getUserId();
    CommonPageInfo<LaborFavouriteEntity> pageInfo = PageUtils.startPage(query.getPageNum(),
        query.getPageSize());
    pageInfo.initData(Optional.ofNullable(laborFavouriteService.getByUserId(userId))
        .orElseGet(ArrayList::new));
    final CommonPageInfo<ProjectSimple> convert = pageInfo.convert(this::toSimple);
    //final List<LaborFavouriteEntity> list = laborFavouriteService.getByUserId(userId);
    if (CollUtil.isEmpty(pageInfo.getList())) {
      return convert;
    }
    final List<String> projectIds = pageInfo.getList().stream()
        .map(LaborFavouriteEntity::getProjectId)
        .collect(Collectors.toList());
    convert.setList(projectBusiness.simpleListByIds(projectIds));
    return convert;
  }

  private ProjectSimple toSimple(LaborFavouriteEntity entity) {
    return new ProjectSimple().setId(entity.getProjectId());
  }

  public void preferFavourite(FavouriteSave favouriteSave) {
    final Long userId = UserUtil.getUserId();
    //校验数据,判断用户是否有收藏过该项目
    final LaborFavouriteEntity entity = Optional.ofNullable(
            laborFavouriteService.getByUserIdAndProjectId(userId, favouriteSave.getProjectId()))
        .orElseGet(LaborFavouriteEntity::new);
    entity.setUpdateBy(userId)
        .setLaborId(userId)
        .setProjectId(favouriteSave.getProjectId())
        .setCreateBy(Optional.ofNullable(entity.getCreateBy()).orElse(userId))
        .setCreateTime(Optional.ofNullable(entity.getCreateTime()).orElse(LocalDateTime.now()))
        .setUpdateTime(LocalDateTime.now())
        .setDeleteFlag(YesNoEnum.isTrue(favouriteSave.getIsFavourite()) ? 0 : 1);
    laborFavouriteService.saveOrUpdate(entity);
  }

  public Long getCollectCount() {
    return laborFavouriteService.getCollectCount(UserUtil.getUserId());
  }
}
